package com.xian.im.modules.command.group.handler;

import com.xian.im.common.constant.CommandCodeConstant;
import com.xian.im.common.model.dto.GroupDto;
import com.xian.im.common.model.session.SessionGroup;
import com.xian.im.common.model.session.SessionUser;
import com.xian.im.common.proto.MsgRequest;
import com.xian.im.common.proto.MsgResponse;
import com.xian.im.common.utils.bussiness.GroupContextUtil;
import com.xian.im.common.utils.bussiness.XimResponseUtil;
import com.xian.im.common.utils.sys.SpringContextHolder;
import com.xian.im.modules.command.group.AbsGroupCommand;
import com.xian.im.modules.im.core.IGroupUserCoreService;
import com.xian.im.modules.message.GroupHandler;

/**
 * @program: xian-im-server
 * @description: 设置管理员
 * @author: liru.xian
 * @create: 2021-01-28 22:53
 **/
public class GroupManager extends AbsGroupCommand {

    @Override
    public void handler(MsgRequest request, String ctx, GroupDto groupEntity) {
        SpringContextHolder.getBean(IGroupUserCoreService.class).addManager(request.getToAccount(),groupEntity.getGroupUniqueId());
        SessionGroup sessionGroup = GroupContextUtil.getSessionGroup(groupEntity.getGroupUniqueId());
        sessionGroup.getManagers().add(request.getToAccount());
        SessionUser managerSession = userChannelClient().session(ctx);
        MsgResponse response = XimResponseUtil.buildSuccess(managerSession);
        // todo 内部自己维护 ctxid:userId 关系
        request.getToAccount();
        SessionUser sessionUser = userChannelClient().sessionUser(request.getToAccount());
        response.setBody(managerSession.getNickname() + " 被" + sessionUser.getNickname() + "设置管理员");
        response.setGroupUniqueId(groupEntity.getGroupUniqueId());
        GroupHandler.groupSendAll(response, groupEntity.getGroupUniqueId());
    }

    /**
     * 群组code
     *
     * @return
     */
    @Override
    public String cmdCode() {
        return CommandCodeConstant.GROUP_MANAGER;
    }
}
